package com.example.lib.extend

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import com.example.lib.R
import com.example.lib.constants.JumpConstants
import com.example.lib.databinding.EmptyLayoutBinding
import java.io.Serializable

fun Activity.getIntentString(key: String, default: String = ""): String {

    return try {
        val bundleExtra = intent.extras
        bundleExtra?.getString(key) ?: default
    } catch (_: Exception) {
        default
    }
}

fun Activity.getIntentStringArray(key: String, default: ArrayList<String> = ArrayList()): ArrayList<String> {
    return try {
        val bundleExtra = intent.extras
        bundleExtra?.getStringArrayList(key) ?: default
    } catch (_: Exception) {
        default
    }
}

fun <T : Serializable> Activity.getIntentSerializable(key: String): T? {
    return try {
        val bundleExtra = intent.extras ?: return null
        val serializable: T? = bundleExtra.getSerializable(key) as T?
        serializable
    } catch (_: Exception) {
        null
    }
}

fun <T : Parcelable> Activity.getIntentParcelable(key: String): T? {
    return try {
        val bundleExtra = intent.extras ?: return null
        val parcelable: T? = bundleExtra.getParcelable(key)
        parcelable
    } catch (_: Exception) {
        null
    }
}

fun <T : Parcelable> Activity.getIntentParcelableArray(key: String): ArrayList<T>? {
    return try {
        val bundleExtra = intent.extras ?: return null
        val parcelable: ArrayList<T>? = bundleExtra.getParcelableArrayList(key)
        parcelable
    } catch (_: Exception) {
        null
    }
}

fun Activity.getIntentInt(key: String, default: Int = 0): Int {
    return try {
        val bundleExtra = intent.extras ?: return default
        return bundleExtra.getInt(key, default)
    } catch (_: Exception) {
        default
    }
}

fun Activity.getIntentLong(key: String, default: Long = 0): Long {
    return try {
        val bundleExtra = intent.extras ?: return default
        return bundleExtra.getLong(key, default)
    } catch (_: Exception) {
        default
    }
}

fun Activity.getIntentBoolean(key: String, default: Boolean = false): Boolean {
    return try {
        val bundleExtra = intent.extras ?: return default
        return bundleExtra.getBoolean(key, default)
    } catch (_: Exception) {
        default
    }
}

fun Activity.getBundle(): Bundle {
    return try {
        return intent.extras ?: Bundle()
    } catch (_: Exception) {
        Bundle()
    }
}

fun Intent.getIntentString(key: String, default: String = ""): String {
    return try {
        val bundleExtra = extras ?: return default
        return bundleExtra.getString(key) ?: default

    } catch (_: Exception) {
        default
    }
}

fun Intent.getIntentInt(key: String, default: Int = 0): Int {
    return try {
        val bundleExtra = extras ?: return default
        return bundleExtra.getInt(key, default)
    } catch (_: Exception) {
        default
    }
}

fun Intent.getIntentLong(key: String, default: Long = 0): Long {
    return try {
        val bundleExtra = extras ?: return default
        return bundleExtra.getLong(key, default)
    } catch (_: Exception) {
        default
    }

}

fun Intent.getIntentBoolean(key: String, default: Boolean = false): Boolean {
    return try {
        val bundleExtra = extras ?: return default
        bundleExtra.getBoolean(key, default)
    } catch (_: Exception) {
        default
    }
}

fun <T : Serializable> Intent.getIntentSerializable(key: String): T? {
    return try {
        val serializable: T? = this.getSerializableExtra(key) as T?
        serializable
    } catch (_: Exception) {
        null
    }
}

fun <T : Parcelable> Intent.getIntentParcelable(key: String): T? {
    return try {
        val bundleExtra = extras ?: return null
        val parcelable: T? = bundleExtra.getParcelable(key)
        parcelable
    } catch (_: Exception) {
        null
    }
}

fun Intent.getBundle(): Bundle {
    return try {
        extras ?: Bundle()
    } catch (_: Exception) {
        Bundle()
    }
}

fun Activity.getRequestCode(): Int = getIntentInt(JumpConstants.RequestCode)

/**
 * 缺省图显示Activity
 *@ids 一定要使用intArrayOf()
 */
fun Activity.goneLayout(views: MutableList<View>,
    text: String,
    res: Int = R.mipmap.icon_empty,
    state: Int = View.GONE,
    genBack: Int = 0,
    genMargin: Int = 0) {
    val viewGroup: ViewGroup = findViewById(android.R.id.content)
    if (viewGroup.tag != null) return
    val binding = EmptyLayoutBinding.bind(LayoutInflater.from(this).inflate(R.layout.empty_layout, viewGroup, false))
    viewGroup.removeView(binding.root)
    viewGroup.addView(binding.root)

    if (genBack != 0) binding.root.setBackgroundResource(genBack)

    if (genMargin > 0) {
        val margin = dip2px(genMargin.toFloat())
        binding.root.setMargin(margin, margin, margin, margin)
    }

    if (res == 0) {
        binding.EmptyLayoutImg.gone()
    } else {
        binding.EmptyLayoutImg.visi()
        binding.EmptyLayoutImg.loadImage(res)
    }

    binding.EmptyLayoutText.also {
        if (text.isEmpty()) it.visibility = state
        else {
            it.visi()
            it.text = text
        }
    }

    views.forEach {
        it.gone()
    }
    viewGroup.tag = binding.root
}

/**
 * 缺省图显示
 * 警告，外部不要使用RelativeLayout包裹，会出现堆叠问题,
 */
fun Activity.goneLayout(
    view: View,
    views: MutableList<View>,
    text: String,
    res: Int = R.mipmap.icon_empty,
    genBack: Int = 0,
    genMargin: Int = 0,
) {
    if (view is ViewGroup) {
        if (view.tag != null) return
        val binding = EmptyLayoutBinding.bind(LayoutInflater.from(this).inflate(R.layout.empty_layout, view, false))
        view.removeView(binding.root)
        view.addView(binding.root)
        binding.root.layoutParamsMatchParent()

        if (genBack != 0) binding.root.setBackgroundResource(genBack)

        if (genMargin > 0) {
            val margin = dip2px(genMargin.toFloat())
            binding.root.setMargin(margin, margin, margin, margin)
        }

        if (res == 0) binding.EmptyLayoutImg.gone()
        else {
            binding.EmptyLayoutImg.visi()
            binding.EmptyLayoutImg.loadImage(res)
        }

        binding.EmptyLayoutText.also {
            if (text.isEmpty()) it.gone()
            else {
                it.visi()
                it.text = text
            }
        }

        views.forEach {
            it.gone()
        }
        view.tag = binding.root
    }
}

/**
 * 缺省图移除
 */
fun Activity.visibleLayout(views: MutableList<View>) {
    val viewGroup: ViewGroup = findViewById(android.R.id.content)
    if (viewGroup.tag == null) {
        val relative = View.inflate(this, R.layout.empty_layout, null) as ConstraintLayout
        viewGroup.removeView(relative)
    } else {
        if (viewGroup.tag != null) {
            if (viewGroup.tag is View) {
                viewGroup.removeView(viewGroup.tag as View)
            }
        }
        viewGroup.tag = null
    }
    views.forEach {
        it.visi()
    }
}

/**
 * 缺省图移除
 */
fun Activity.visibleLayout(view: View, views: MutableList<View>) {
    if (view is ViewGroup) {
        val relative = LayoutInflater.from(this).inflate(R.layout.empty_layout, view, false) as ConstraintLayout
        if (view.tag == null) {
            view.removeView(relative)
        } else {
            if (view.tag != null) {
                if (view.tag is View) {
                    view.removeView(view.tag as View)
                }
            }
            view.tag = null
        }
        views.forEach {
            it.visi()
        }
    }
}